package com.longcai.entity.payment.weixin.response;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;

/**
 * Ajax 返回工具类
 */
public class Result {

    public static final String RETURN_MSG = "msg";
    public static final String RETURN_RESULT = "result";
    public static final String RETURN_OBJ = "obj";
    public static final String RETURN_CODE = "code";

    public static JSONObject returnSuccessMsg(Object obj) {
        JSONObject r = new JSONObject();
        r.put(RETURN_RESULT, true);
        r.put(RETURN_MSG, "success");
        r.put(RETURN_OBJ, obj);
        return r;
    }

    public static JSONObject returnFailMsg(Object msg) {
        JSONObject r = new JSONObject();
        r.put(RETURN_RESULT, false);
        r.put(RETURN_MSG, msg);
        r.put(RETURN_OBJ, null);
        return r;
    }

    public static JSONObject returnFailMsg(Object msg, Object obj) {
        JSONObject r = new JSONObject();
        r.put(RETURN_RESULT, false);
        r.put(RETURN_MSG, msg);
        r.put(RETURN_OBJ, obj);
        return r;
    }

    public static JSONObject returnMsg(Object result, Object msg, Object obj) {
        JSONObject r = new JSONObject();
        r.put(RETURN_RESULT, result);
        r.put(RETURN_MSG, msg);
        r.put(RETURN_OBJ, obj);
        return r;
    }

    public static HashMap<String, Object> returnMap(Object result, Object msg, Object obj) {
        HashMap<String, Object> r = new HashMap<String, Object>();
        r.put(RETURN_RESULT, result);
        r.put(RETURN_MSG, msg);
        r.put(RETURN_OBJ, obj);
        return r;
    }

    public static JSONObject returnCode(Object result, Object code,  Object obj) {
        JSONObject r = new JSONObject();
        r.put(RETURN_RESULT, result);
        r.put(RETURN_MSG, code);
        r.put(RETURN_OBJ, obj);
        return r;
    }

    public static boolean isSuccess(JSONObject result) {
        if (result == null) return false;
        return result.getBooleanValue(RETURN_RESULT);
    }

    public static <T> T getObj(JSONObject result, Class<T> clazz) {
        if (result == null) return null;
        return result.getObject(RETURN_OBJ, clazz);
    }

    public static String getMsg(JSONObject result) {
        if (result == null) return null;
        return result.getString(RETURN_MSG);
    }


}
